//
//  ViewController.m
//  UI_18_homework_SQLite_联系人信息存入数据库
//
//  Created by li cheng on 16/8/8.
//  Copyright © 2016年 li cheng. All rights reserved.
//

#import "ViewController.h"
#import <AssetsLibrary/AssetsLibrary.h>
#import "Contact.h"
#import "SQLiteDataBaseManager.h"
@interface ViewController ()<UINavigationControllerDelegate, UIImagePickerControllerDelegate>
@property (weak, nonatomic) IBOutlet UIButton *imageButton;
@property (nonatomic, strong) UIImage *image;
@property (weak, nonatomic) IBOutlet UITextField *name;
@property (weak, nonatomic) IBOutlet UITextField *phoneNumber;
@end

@implementation ViewController
{
    NSString *filePath;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.navigationItem.title = @"联系人保存信息";
    self.navigationController.navigationBar.translucent = NO;
//    self.imageButton.currentBackgroundImage =
    
    
}
#pragma mark - 弹出图片选择器
- (IBAction)showIamgePickerVC:(id)sender {
    UIImagePickerController *imagePickerVC = [[UIImagePickerController alloc] init];
    imagePickerVC.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
    imagePickerVC.allowsEditing = YES;
    imagePickerVC.delegate = self;
    [self presentViewController:imagePickerVC animated:YES completion:^{
        
    }];
}
//当选择一张图片后进入这里
-(void)imagePickerController:(UIImagePickerController*)picker didFinishPickingMediaWithInfo:(NSDictionary *)info

{
    NSString *type = [info objectForKey:UIImagePickerControllerMediaType];
    //当选择的类型是图片
    if ([type isEqualToString:@"public.image"])
    {
        //先把图片转成NSData
        self.image = [info objectForKey:@"UIImagePickerControllerOriginalImage"];
        //关闭相册界面
        [picker dismissViewControllerAnimated:YES completion:^{
            
        }];
        [_imageButton setImage:_image forState:UIControlStateNormal];
        
    } 
    
}

- (IBAction)save:(id)sender {
    NSData *data;
    if (UIImagePNGRepresentation(_image) == nil)
    {
        data = UIImageJPEGRepresentation(_image, 1.0);
    }
    else
    {
        data = UIImagePNGRepresentation(_image);
    }
    // 找到对应的沙盒路径
    NSArray *array = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, NO);
    NSString *documentsPath = [array lastObject];
    NSString *pathImages = [documentsPath stringByAppendingPathComponent:@"images"];
    NSString *pathImage = [pathImages stringByAppendingPathComponent:_name.text];
    [data writeToFile:pathImage atomically:NO];
    Contact *contact = [[Contact alloc] init];
    contact.contactId = 0;
    contact.imageFile = pathImage;
    contact.name = _name.text;
    contact.phoneNumber = _phoneNumber.text;
    SQLiteDataBaseManager *dataBaseManager = [SQLiteDataBaseManager sharedManger];
    [dataBaseManager openSQLite3];
    [dataBaseManager cerateSQLite3];
    [dataBaseManager insertContact:contact];
}




- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


@end
